package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.FeekbackReponse;
import com.topboya.linker.model.response.TaskResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.FeekbackView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FeekbackPresenter extends BasePresenter<FeekbackView> {

    private static final String TAG = "FeekbackPresenter-";

    public FeekbackPresenter(BaseActivity context) {
        super(context);
    }

    public void getFeekbackLable(String type) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getFeekbackLable(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(feekbackReponse -> {
                    String success = feekbackReponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        List<FeekbackReponse.FeekbackData> feekbackData = feekbackReponse.getData();
                         getView().getFeekbackLable(feekbackData);

                    }
                }, this::loginError);
    }

    public void getTask(String taskid){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getTask(taskid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(taskResponse -> {
                    String success = taskResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

                        TaskResponse.Data data = taskResponse.getData();
                        getView().getTask(data);

                    }

                }, this::loginError);
    }

    //    String operaId 这个是签出需要的参数
    public void updateTaskBack(String operaId,String sumUp, String feedbackId, String Id, String signOutLat, String signOutLon) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        LogUtils.v(TAG +"operaId:"+operaId+",sumUp:"+sumUp+",feedbackId:"+feedbackId+",Id"+Id+",signOutLat:"+signOutLat+",signOutLon:"+signOutLon);
        ApiRetrofit.getInstance().updateTaskBack(sumUp, feedbackId,Id,signOutLat,signOutLon)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(updateTaskBackResponse -> {
                    String success = updateTaskBackResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {
                       LogUtils.v(TAG + "updateTaskBackResponse:"+updateTaskBackResponse.toString());
                       UIUtils.showToast("反馈成功");
                        miniSignOut(Id,operaId,signOutLat,signOutLon);
                    }else {
                        UIUtils.showToast("反馈失败");

                    }
                }, this::loginError);
    }


    public void miniSignOut(String id, String operaId, String signOutLat,String signOutLon) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().miniSignOut( id,operaId, signOutLat,signOutLon)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(miniSignOutReponse -> {
                    String success = miniSignOutReponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {
                        UIUtils.showToast("签出成功");
                        LogUtils.v(TAG + "miniSignOutReponse:"+miniSignOutReponse.toString());
                        getView().miniSignOut();
                    }else {

                        UIUtils.showToast("签出失败");
                    }
                }, this::loginError);
    }


    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
